package org.springblade.modules.restaurant.job;

import cn.hutool.core.date.DateUtil;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import com.aizuda.snailjob.common.log.SnailJobLog;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.restaurant.entity.Task;
import org.springblade.modules.restaurant.service.ITaskService;
import org.springframework.stereotype.Component;

/**
 * 生成任务
 *
 * @author harry
 * @公众号 Harry技术
 */
@Slf4j
@Component
@AllArgsConstructor
@JobExecutor(name = "taskGenJobExecutor")
public class TaskGenJobExecutor {

	private final ITaskService taskService;

	public ExecuteResult jobExecute(JobArgs jobArgs) {
		SnailJobLog.REMOTE.info("生成任务===>任务周期 ,jobArgs:{}", jobArgs);
		taskService.genTaskRecord();

		// 移除昨天以及之前未下发的任务
		taskService.remove(Wrappers.<Task>lambdaQuery().le(Task::getTaskTime, DateUtil.formatDate(DateUtil.yesterday())));

		return ExecuteResult.success();
	}

//	@PostConstruct
	public void init() {
		log.info("生成任务启动");
		taskService.genTaskRecord();
		// 移除昨天以及之前未下发的任务
		taskService.remove(Wrappers.<Task>lambdaQuery().le(Task::getTaskTime, DateUtil.formatDate(DateUtil.yesterday())));

	}
}
